<?php
/**
 * Created by PhpStorm.
 * User: nmw
 * Date: 17-10-17
 * Time: 上午11:34
 */

if (!function_exists('showErrorMsg'))
{
    /**
     * 错误提示函数
     *
     * @param $msgCode
     * @param array $msgData
     */
    function showErrorMsg($msgCode, $msgData = [])
    {
        $msgArr = C('SIMPLE_CHINA');
        $message = '';

        if (!isset($msgArr[$msgCode]))
        {
            $msgCode = 10000;
        }
        else
        {
            if (!empty($msgData))
            {
                foreach ($msgData as $mdKey => $mdItem)
                {
                    $message = str_replace("#{$mdKey}#", $mdItem, $msgArr[$msgCode]);
                }
            }
        }

        $return = [
            'code'    => $msgCode,
            'message' => !empty($message) ? $message : $msgArr[$msgCode],
            'data'    => [],
        ];

        header('Content-type: application/json');
        exit(json_encode($return));
    }
}

if (!function_exists('showMsg'))
{
    /**
     * 返回提示函数
     *
     * @param array $data
     */
    function showMsg($data = [])
    {
        $return = [
            'code'    => 0,
            'message' => 'success',
            'data'    => $data,
            'isEmpty' => !empty($data) ? FALSE : TRUE,
        ];

        header('Content-type: application/json');
        exit(json_encode($return));
    }
}

if (!function_exists("doPost"))
{
    /**
     * 数据传输POST
     *
     * @param $url
     * @param array $data
     * @return mixed
     */
    function doPost($url, $data = [])
    {
        if (empty($data))
        {
            $data = [];
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
        curl_setopt($ch, CURLOPT_URL, $url);
        $ret = curl_exec($ch);
        curl_close($ch);

        return $ret;
    }
}

if (!function_exists("bdEncrypt"))
{
    /**
     * GCJ-02(火星，高德) 坐标转换成 BD-09(百度) 坐标
     *
     * @param $ggLon
     * @param $ggLat
     * @return mixed
     */
    function bdEncrypt($ggLon, $ggLat)
    {
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $x = $ggLon;
        $y = $ggLat;
        $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
        $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
        $data['bd_lon'] = $z * cos($theta) + 0.0065;
        $data['bd_lat'] = $z * sin($theta) + 0.006;

        return $data;
    }
}

if (!function_exists("bdDecrypt"))
{
    /**
     * BD-09(百度) 坐标转换成  GCJ-02(火星，高德) 坐标
     *
     * @param $bdLon
     * @param $bdLat
     * @return mixed
     */
    function bdDecrypt($bdLon, $bdLat)
    {
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $x = $bdLon - 0.0065;
        $y = $bdLat - 0.006;
        $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
        $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
        $data['gg_lon'] = $z * cos($theta);
        $data['gg_lat'] = $z * sin($theta);

        return $data;
    }
}


